﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShopOnline.Models.DAL;
using ShopOnline.Models;
using ShopOnline.Models.Utils;

namespace ShopOnline.Controllers
{
    public class ProductCatelogyController : Controller
    {
        private ShopOnline.Models.ShopOnlineDBDataContext db;
        private DAL_ProductCatelogy ProductCatelogyRepository;
        private static int productCatelogycurent;

        public int ProductCatelogycurent
        {
            get { return productCatelogycurent; }
            set { productCatelogycurent = value; }
        }
        public ProductCatelogyController()
        {
            db = new Models.ShopOnlineDBDataContext();
            ProductCatelogyRepository = new DAL_ProductCatelogy();
        }


        //
        // GET: /ProductCatelogy/
         [Authorize]
        public ActionResult AdminListProductCatelogy()
        {
            var list = ProductCatelogyRepository.getListProductCatelogy();
            return View(list);
        }

        //
        // GET: /ProductCatelogy/Details/5
         [Authorize]
        public ActionResult Details(int id)
        {
            var procate = ProductCatelogyRepository.getProductCatelogyById(id);
            return View(procate);
        }

        //
        // GET: /ProductCatelogy/Create
         [Authorize]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /ProductCatelogy/Create
         [Authorize]
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                ProductCatelogy proCate = new ProductCatelogy();
                proCate.catelogyName = collection["catelogyName"];
                proCate.description = collection["description"];
                proCate.piority = int.Parse(collection["piority"].Trim());
                var show = collection["show"];
                if (show == "false")
                {
                    proCate.show = false;
                }
                else
                {
                    proCate.show = true;
                }
                #region upload images
                Uploader uploader = new Uploader();
                uploader.UploadPath = Server.MapPath("~\\Content\\Logo");
                uploader.IsLowerName = false;
                uploader.IsEncryptName = false;
                uploader.IsOverwrite = false;
                uploader.Prefix = "";
                uploader.Suffix = "";

                uploader.AllowedExtensions.Add(".jpg");
                uploader.AllowedExtensions.Add(".jpeg");
                uploader.AllowedExtensions.Add(".gif");
                uploader.AllowedExtensions.Add(".png");

                bool success = uploader.DoUpload("MyFile");
                #endregion
                proCate.logo = uploader.PostedFile.FileName;
                db.ProductCatelogy_add(proCate.catelogyName, proCate.description, proCate.logo, proCate.piority, proCate.show);
                return RedirectToAction("AdminListProductCatelogy");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /ProductCatelogy/Edit/5
  [Authorize]
        public ActionResult Edit(int id)
        {
            var procate = ProductCatelogyRepository.getProductCatelogyById(id);
            return View(procate);
        }

        //
        // POST: /ProductCatelogy/Edit/5
 [Authorize]
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                ProductCatelogy proCate = ProductCatelogyRepository.getProductCatelogyById(id);
                proCate.catelogyName = collection["catelogyName"];
                proCate.description = collection["description"];
                proCate.piority = int.Parse(collection["piority"].Trim());
                var show = collection["show"];
                if (show == "false")
                {
                    proCate.show = false;
                }
                else
                {
                    proCate.show = true;
                }

                db.ProductCatelogy_update(proCate.catelogyId,proCate.catelogyName, proCate.description, proCate.logo, proCate.piority, proCate.show);
                return RedirectToAction("AdminListProductCatelogy");

            }
            catch
            {
                var procate = ProductCatelogyRepository.getProductCatelogyById(id);
                return View(procate);
            }
        }

        //
        // GET: /ProductCatelogy/Delete/5
  [Authorize]
        public ActionResult Delete(int id)
        {
            var procate = ProductCatelogyRepository.getProductCatelogyById(id);
            return View(procate);
        }

        //
        // POST: /ProductCatelogy/Delete/5
 [Authorize]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var procate = ProductCatelogyRepository.getProductCatelogyById(id);
                ProductCatelogyRepository.DeleteProductCatelogy(procate);
                return RedirectToAction("AdminListProductCatelogy");
            }
            catch
            {
                var procate = ProductCatelogyRepository.getProductCatelogyById(id);
                return View(procate);
            }
        }
 [Authorize]
        public ActionResult EditImage(int id)
        {
            try
            {
                productCatelogycurent = id;
                var procate = ProductCatelogyRepository.getProductCatelogyById(id);
                return View(procate);
            }
            catch (Exception)
            {
                return RedirectToAction("AdminListPicture");
            }

        }
 [Authorize]
        [HttpPost]
        public ActionResult EditImages()
        {
            try
            {
                var procate = ProductCatelogyRepository.getProductCatelogyById(productCatelogycurent);
                #region upload images
                Uploader uploader = new Uploader();
                uploader.UploadPath = Server.MapPath("~\\Content\\Logo");
                uploader.IsLowerName = false;
                uploader.IsEncryptName = false;
                uploader.IsOverwrite = false;
                uploader.Prefix = "";
                uploader.Suffix = "";

                uploader.AllowedExtensions.Add(".jpg");
                uploader.AllowedExtensions.Add(".jpeg");
                uploader.AllowedExtensions.Add(".gif");
                uploader.AllowedExtensions.Add(".png");

                bool success = uploader.DoUpload("MyFile");
                #endregion
                string abc = uploader.PostedFile.FileName;
                db.ProductCatelogy_update(procate.catelogyId, procate.catelogyName, procate.description, abc, procate.piority, procate.show);
                return RedirectToAction("Edit", new { id = productCatelogycurent });
            }
            catch (Exception)
            {
                return RedirectToAction("EditImage", new { id = productCatelogycurent });
            }

        }
    }
}
